/**
 * 城市三级联动以及联合获取地区行政编码
 */
const prov_number = document.getElementById('prov-number');
const city_number = document.getElementById('city-number');
const country_number = document.getElementById('country-number');
const prov_code = document.getElementById('provcode');
const city_code = document.getElementById('citycode');
const country_code = document.getElementById('countrycode');
const addr_show_number = document.getElementById('addr-show-number')
const btn_number = document.getElementsByClassName('met04')[0];
/*用于保存当前所选的省市区名字*/
let current04 = {
    prov_number: '',
    city_number: '',
    country_number: ''
};

var  shequlist=[];
/*自动加载省份列表*/
(function showProv() {
    btn_number.disabled = true;
    for (let key in cityList) {
        let provOpt = document.createElement('option');
        provOpt.innerText = provOpt.value = key;
        prov_number.appendChild(provOpt);
    }
})();

/*根据所选的省份来显示城市列表*/
function showCity(obj) {
    let val = obj.options[obj.selectedIndex].value;
    if (val != current04.prov_number) {
        current04.prov_number = val;
        // addr_show_number.innerHTML = addr_show_number.value = '';
        btn_number.disabled = true;
        city_number.length = 1;
        country_number.length = 1;
    }

    if (val != '') {
        for (let key in cityList[current04.prov_number]['city']) {
            let cityOpt = document.createElement('option');
            cityOpt.innerText = cityOpt.value = key;
            city_number.appendChild(cityOpt);
        }
    }
}

/*根据所选的城市来显示县区列表*/
function showCountry(obj) {
    let val = obj.options[obj.selectedIndex].value;
    if (val != current04.city_number) {
        current04.city_number = val;
        // addr_show_number.innerHTML = addr_show_number.value = '';
        country_number.length = 1; //清空之前的内容只留第一个默认选项
        btn_number.disabled = true;
    }
    if (val != '') {
        for (const key in cityList[current04.prov_number]['city'][current04.city_number]['country']) {
            const countryOpt = document.createElement('option');
            countryOpt.innerText = countryOpt.value = key
            country_number.appendChild(countryOpt);
        }
    }
}

/*选择县区之后的处理函数*/
function selecCountry(obj) {
	$("#shequ").find('option').remove();
    current04.country_number = obj.options[obj.selectedIndex].value;
    if (current04.prov_number != '' && current04.city_number != '' && current04.country_number != '') {
        btn_number.disabled = false;
    }
	var provcode=cityList[current04.prov_number]['code'];
	var citycode=cityList[current04.prov_number]['city'][current04.city_number]['code'];
	var countrycode =cityList[current04.prov_number]['city'][current04.city_number]['country'][current04.country_number]['code'];
	prov_code.value=provcode;
	city_code.value=citycode;
	country_code.value=countrycode;
	console.log(provcode)
	axios.get(url+"/community/getShequList?provinceCode="+provcode+"&cityCode="+citycode+"&districtCode="+countrycode)
	.then(res=>{
		console.log(res.data)
		shequlist=res.data;
		option2 = new Option('请选择社区', 0);
		$('#shequ').append(option2);
		$.each(res.data, function (index, item) {
		        var option;
		        if(item.areaInfo!=null||item.areaInfo!=""||item.areaInfo!="null"){
		            option = new Option(item.areaInfo, item.id);
		        }
		        $('#shequ').append(option);// 下拉菜单里添加元素
				
		});
		form.render("select");
	})
	.catch((error)=>{
	    //报错
	});
}

function selecsSq(){
	var sqfzr=document.getElementById("sqfzr");
	var sqdh=document.getElementById("sqdh");
	var areaInfoId=document.getElementById("areaInfoId");
	var myselect=document.getElementById("shequ");
	var index=myselect.selectedIndex ; //
	var id =  myselect.options[index].value;
	$.each(shequlist, function (index, item) {
	      if (item.id==id) {
			areaInfoId.value=item.id;
	      	sqfzr.value=item.leaderName;
	      	sqdh.value=item.leaderPhone;
	      } 
	});
}
/*点击确定按钮显示用户所选的地址*/
function showAddr() {
	
	var provcode=cityList[current04.prov_number]['code'];
	var citycode=cityList[current04.prov_number]['city'][current04.city_number]['code'];
	var countrycode =cityList[current04.prov_number]['city'][current04.city_number]['country'][current04.country_number]['code'];
	var myselect=document.getElementById("shequ");
	var index=myselect.selectedIndex ; //
	var id =  myselect.options[index].value;
	console.log(provcode)
	console.log(citycode)
	console.log(countrycode)
	console.log(id)
	axios.get(url+"/community/pcdGetUserNum?provinceCode="+provcode+"&cityCode="+citycode+"&districtCode="+countrycode+"&communityId="+id)
	.then(res=>{
		console.log(res.data)
	})
	.catch((error)=>{
	    //报错
	});
   
}